<?php

/**
 * 控制器基类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CController.php 2009-07-28 09:15:33Z andery $
 */
class CController
{
	// 视图实例对象
    protected $view = null;
    
	public function __construct(){
		$this->_init_session();
        header("Content-type: text/html; charset=" . Nimo::app()->charset);
	}
	
	/**
	 * 初始化session
	 */
	public function _init_session()
	{
	    Nimo::import('CSession');
	    new CSession();
	}
	
	/**
	 * 初始化视图连接
	 */
	private function _init_view()
	{
	    $this->view = CView::getInstance();
	    $this->_config_view();
	}
	
	/**
	 * 配置视图，通过子类重写此方法实现多模板
	 * 
	 */
	protected function _config_view()
	{
	    $this->view->set_view_config();
	}
	
	/**
	 * 显示视图
	 * @param string $tplfile 视图名
	 * @param array $tpl_data 传递给视图的变量数组
	 */
	protected function display($tplfile, $tpl_data)
	{
	    $this->_init_view();
		$this->view->display($tplfile, $tpl_data);
	}
	
	/**
	 * 设置视图变量
	 * 
	 */
	protected function assign($tpl_data, $value = '')
    {
        $this->_init_view();
        if (!is_array($tpl_data) && $tpl_data != '') {
            $this->view->assign($tpl_data, $value);
        } else {
            $this->view->assign($tpl_data);
        }
    }
    
    /**
     * 表单令牌验证
     */
    public function checkToken()
    {
        $token_name = CConf::get('token.token_name');
        if (isset($_REQUEST[$token_name]) && $_REQUEST[$token_name]!='') {
            $token_value = $_REQUEST[$token_name];
            $tv_arr = explode('#', $token_value);
            $custom_name = $tv_arr['0'];
            if ($_REQUEST[$token_name] == $_SESSION[$token_name][$custom_name]) {
            	unset($_SESSION[$token_name][$custom_name], $tv_arr);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    /**
     * 检查PHP版本
     */
    public function check_php_version()
    {
        if (PHP_VERSION >= 5.0) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 检查GD库
     */
    public function check_gd()
    {
        $gd_info = function_exists('gd_info') ? gd_info() : array();
        if (isset($gd_info['GD Version']) && $gd_info['GD Version']) {
            return true;
        } else {
            return false;
        }
    }
}